#import "BargainSaleViewController.h"

@implementation BargainSaleViewController

- (void)awakeFromNib {
	discountValue = 0.2f;
}

- (void)calc {
	[discountPercent setText:[NSString stringWithFormat:@"%d", (int)(discountValue * 100)]];
	int p_org = [[priceOriginal text] intValue];
	if(p_org > 0){
		[priceNew setText:[NSString stringWithFormat:@"%d", (int)(p_org * (1.f - discountValue))]];
	}
}

- (IBAction)buttonValueChanged:(id)sender {
	int idx = [sender selectedSegmentIndex];
	switch(idx){
		case 0:
			discountValue = 0.2f;
			break;
		case 1:
			discountValue = 0.3f;
			break;
		case 2:
			discountValue = 0.4f;
			break;
		case 3:
			discountValue = 0.5f;
			break;
		default:
			break;
	}
	[discountSlider setValue:discountValue animated:YES];
	[self calc];
}

- (IBAction)escapButtonPush:(id)sender {
    [priceOriginal endEditing:YES];
}

- (IBAction)sliderValueChanged:(id)sender {
	float val = [(UISlider*)sender value];
	discountValue = (int)(val * 20) / 20.f;
	if(discountValue == 0.2f){
		[discountButton setSelectedSegmentIndex:0];
	}
	else if(discountValue == 0.3f){
		[discountButton setSelectedSegmentIndex:1];
	}
	else if(discountValue == 0.4f){
		[discountButton setSelectedSegmentIndex:2];
	}
	else if(discountValue == 0.5f){
		[discountButton setSelectedSegmentIndex:3];
	}
	else{
		[discountButton setSelectedSegmentIndex:-1];
	}
	[self calc];
}

- (IBAction)onEndEdit:(id)sender {
    [sender resignFirstResponder];
	[self calc];
}

- (IBAction)pushAddCart:(id)sender
{
	int org = [[cartPrice text] intValue];
	int add = [[priceNew text] intValue];
	[cartPrice setText:[NSString stringWithFormat:@"%d", org + add]];
}

- (IBAction)pushClearCart:(id)sender
{
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"カートをクリアしますか？" delegate:self cancelButtonTitle:@"キャンセル" destructiveButtonTitle:@"クリアする" otherButtonTitles:nil];
	actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
	[actionSheet showInView:self.view];
	[actionSheet release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if(buttonIndex == 0){
		[cartPrice setText:[NSString stringWithFormat:@"%d", 0]];
	}
}

@end
